<template>
    <div class="shop_car">
        <!-- 头部 -->
        <div class="shop_head">
            <p class="car_tit">购物车</p>
            <div class="btn_flag" @click="toggle">{{ flag ? '编辑' : '完成' }}</div>
        </div>
        <!-- 商品显示隐藏 -->
        <div class="car_content" v-show="!shopStore.isHidden">
            <div class="icon_img">
                <van-icon name="shopping-cart-o" size="5rem" color="#ddd" />
            </div>
            <div class="txt">
                <p>您的购物车空空如也哦~</p>
                <p>去看看心仪的商品吧</p>
            </div>
            <div class="btnn" @click="addClass">立即去购物</div>
        </div>
        <!-- 添加后的商品数据渲染-->
        <div class="shop_lists" v-show="shopStore.isHidden">
            <!-- 展示列表 -->
            <ul class="shop_oul">
                <li class="shop__oul_items" v-for="item, index in shopStore.shopList" :key="item.gId">
                    <div class="items_check">
                        <input type="checkbox" :checked="item.checked" @change="changeItemState(index)"
                            class="check checkShop">
                        <div class="imgs"><img src="../../public/image/11.jpg"></div>
                        <div class="check-tit">{{ item.title }}</div>
                    </div>
                    <div class="items_content">
                        <div class="imgs"><img src="../../public/image/bz2.jpg"></div>
                        <div class="txts">
                            <p class="tit">{{ item.name }}</p>
                            <p class="tit1">型号，规格，颜色</p>
                            <div class="price_count">
                                <div class="price_box"><span class="price">{{ item.price }}</span></div>
                                <div class="count">
                                    <button @click="subNum(index)" class="jian">-</button>
                                    <span>{{ item.count }}</span>
                                    <button @click="addNum(index)" class="jia">+</button>
                                </div>
                            </div>
                            <div class="total">合计:<span>{{ item.count * item.price }}</span></div>
                        </div>
                    </div>
                </li>
            </ul>

            <van-overlay :show="show" @click="show = false">
                <div class="block">
                    <van-loading type="spinner" v-show="show" />
                </div>
            </van-overlay>

            <!-- 结算后显示 -->
            <div class="showtab">
                <van-picker v-show="flag1" :columns="columns" @confirm="onConfirm" @cancel="onCancel()" />
            </div>
            <!-- 全选反选 -->
            <div class="chose">
                <input type="checkbox" v-model="checkShop" class="checked checkall">全选
                <div class="totals" v-show="flag">合计:<span>{{ shopStore.money }}</span></div>
                <div class="final" v-if="flag" @click="finall">结算</div>
                <div class="final" v-show="!flag" @click="removeItems">删除</div>
            </div>
        </div>
    </div>
    <van-tabbar route placeholder="true" v-model="active">
        <van-tabbar-item icon="gem-o" to="/">首页</van-tabbar-item>
        <van-tabbar-item icon="search" to="/classify">分类</van-tabbar-item>
        <van-tabbar-item icon="cart-o" to="/shopp">购物车</van-tabbar-item>
        <van-tabbar-item icon="manager-o" to="/mine">我</van-tabbar-item>
    </van-tabbar>
    <div class="redRoue" v-show="shopStore.isHidden">{{ shopStore.shopList.length }}</div>
</template>
<script setup>
import { showConfirmDialog } from 'vant';
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useShopStore } from '../store/shop'
const shopStore = useShopStore()
const router = useRouter()
let active = ref(0)
let flag = ref(true)
let flag1 = ref(false)
const show = ref(false);
function toggle() {
    flag.value = !flag.value
}
let addNum = (i) => {
    shopStore.addNum(i)
}
let subNum = (i) => {
    shopStore.subNum(i)
}
let changeItemState = (i) => {
    shopStore.changeItemState(i)
}
let finall = () => {
    flag1.value = !flag1.value
}
//结算后操作
const columns = [
    { text: 'Top-Pay', value: 'Top-Pay' },
    { text: '支付宝', value: '支付宝' },
    { text: '微信', value: 'W微信' },
    { text: '银行卡', value: '银行卡' }
];
// 那会儿不是已经显示了  咋回事那会哪显示了
const onConfirm = () => {
    flag1.value = !flag1.value
    show.value = !show.value
    setTimeout(() => {
        shopStore.config()
        show.value = !show.value
    }, 1000)
};
const onCancel = () => {
    flag1.value = !flag1.value
}

let addClass = () => {
    router.push('/classify')
}
//删除操作
let removeItems = () => {
    showConfirmDialog({
        title: '',
        message:
            '确认删除这些商品？',
    })
        .then(() => {
            shopStore.removeItems()
        })
        .catch(() => {

        });
}

let checkShop = computed({
    get() {
        return shopStore.shopList.every(item => {
            return item.checked === true
        })
    },
    set(val) {
        shopStore.shopList.forEach(item => {
            item.checked = val
        })
    }
})
</script>   

<style lang="scss" scoped>
.redRoue {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #d8182d;
    position: fixed;
    bottom: 67px;
    right: 32%;
    z-index: 100;
    color: #fff;
    font-size: 24px;
    text-align: center;
}

.wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.block {
    width: 50px;
    height: 50px;
    position: absolute;
    left: 50%;
    top: 46%;
}

.partition {
    width: 100%;
    height: 190px;
}

/*商品列表*/
.shop_car {
    width: 750px;
}

.shop_lists {
    width: 92%;
    height: auto;
    margin: 0 auto;
    overflow: hidden;

    .shop_oul {
        width: 100%;
        height: 100%;
        box-sizing: border-box;
        background: #fff;
        box-shadow: 2px 3px 20px #ccc;

        .shop__oul_items {
            width: 100%;
            height: auto;
            padding: 20px;

            /*店铺名称*/
            .items_check {
                width: 100%;
                height: 50px;
                display: flex;
                flex-wrap: nowrap;
                justify-content: flex-start;
                align-items: center;

                .check {
                    width: 40px;
                    height: 40px;
                }

                .imgs {
                    width: 50px;
                    height: 50px;
                    background: #ccc;
                    border-radius: 50%;
                    margin: 0 10px;

                    img {
                        width: 100%;
                        height: 100%;
                        border-radius: 50%;
                        display: block;
                    }
                }

                .check-tit {
                    color: #3a3a3a;
                    font-size: 24px;
                }
            }

            .items_content {
                width: 100%;
                height: 280px;
                padding: 20px 0 30px;
                display: flex;
                justify-content: space-around;
                align-items: center;
                box-sizing: border-box;

                .check {
                    width: 40px;
                    height: 40px;
                }

                .imgs {
                    width: 208px;
                    height: 208px;
                    border-radius: 10px;
                    background: #ccc;

                    img {
                        width: 208px;
                        height: 208px;
                        display: block;
                        border-radius: 10px;
                    }
                }

                .txts {
                    box-sizing: border-box;
                    width: 360px;
                    height: 100%;

                    .tit {
                        font-size: 26px;
                        color: #3a3a3a;

                    }

                    .tit1 {
                        color: #ccc;
                        font-size: 26px;
                        margin: 16px 0;
                    }

                    .price_count {
                        margin-top: 30px;
                        width: 93%;
                        height: auto;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        .price {
                            color: #d8182d;
                            font-size: 28px;
                            font-weight: bold;
                        }

                        .count {
                            font-size: 36px;

                            span {
                                padding: 0 10px;
                            }

                            button {
                                outline: none;
                                border: 1px solid #ddd;
                                width: 30px;
                                height: 40px;
                                color: #3a3a3a;
                                background: #fff;
                            }
                        }
                    }

                    .total {
                        width: 100%;
                        color: #3a3a3a;
                        font-size: 28px;
                        text-align: center;

                    }
                }

            }
        }
    }

    .showtab {
        width: 100%;
        height: auto;
        position: fixed;
        bottom: 40px;
        left: 0;
    }


    .chose {
        background: #fff;
        font-size: 36px;
        width: 100%;
        height: 90px;
        position: fixed;
        bottom: 100px;
        left: 0;
        padding: 0 0 0 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .checked {
            width: 40px;
            height: 40px;
        }

        .totals {
            font-size: 34px;
            color: #3a3a3a;

            span {
                color: #d8182d;
                font-weight: bold;
            }
        }

        .final {
            width: 228px;
            height: 100%;
            background: #d8182d;
            color: #fff;
            line-height: 90px;
            text-align: center;
            font-size: 20px;
        }
    }
}


.shop_car {
    width: 100%;
    height: 100vh;
    background: #EFEFF4;

    .shop_head {
        width: 100%;
        height: 90px;
        padding: 10px 20px;
        display: flex;
        justify-content: space-between;
        box-sizing: border-box;
        align-items: center;

        .car_tit {
            color: #3a3a3a;
            font-size: 36px;
            font-weight: bold;
        }

        .btn_flag {
            color: #d8182d;
            font-size: 26px;
            ;
        }
    }

    .car_content {
        width: 100%;
        height: auto;
        padding: 80px 0 100px;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;

        .icon_img {
            width: 320px;
            height: 320px;
            margin: 20px 0 100px;
        }

        .txt {
            width: 100%;
            height: auto;
            color: #3a3a3a;
            font-size: 34px;
            text-align: center;
            margin-bottom: 60px;
        }

        .btnn {
            width: 300px;
            height: 100px;
            font-size: 34px;
            color: #d8182d;
            text-align: center;
            line-height: 100px;
            border-radius: 6px;
            border: 1px solid #d8182d;
        }
    }
}
</style>